package com.tairanchina.md.meeting.service.impl;

import com.tairanchina.md.meeting.dao.YearMeetingAwarderMapperExt;
import com.tairanchina.md.meeting.dao.YearMeetingSunawarderMapperExt;
import com.tairanchina.md.meeting.dao.YearMeetingSungoodsMapperExt;
import com.tairanchina.md.meeting.dto.SunAwarderDto;
import com.tairanchina.md.meeting.entity.*;
import com.tairanchina.md.meeting.service.YearMeetingSunAwarderService;
import com.tairanchina.md.meeting.service.YearMeetingSunGoodsService;
import com.tairanchina.md.meeting.utils.CommonConvertUtil;
import com.tairanchina.md.turntable.dto.YearMeetingSunawarderDto;
import com.txframework.util.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * Created by lxliuxuan on 2016/12/24.
 */
@Transactional
@Service
public class YearMeetingSunAwarderServiceImpl implements YearMeetingSunAwarderService {

    @Resource
    private YearMeetingSunawarderMapperExt sunAwarderDao;
    @Resource
    private YearMeetingSungoodsMapperExt sunGoodsDao;
    @Override
    public YearMeetingSunawarder findAwarderByPhone(String userPhone) {
        YearMeetingSunawarderExample example = new YearMeetingSunawarderExample();
        YearMeetingSunawarderExample.Criteria criteria = example.createCriteria();
        criteria.andUserPhoneEqualTo(userPhone);
        List<YearMeetingSunawarder> yearMeetingSunAwarders = sunAwarderDao.selectByExample(example);
        if(yearMeetingSunAwarders != null && !yearMeetingSunAwarders.isEmpty()){
            return yearMeetingSunAwarders.get(0);
        }
        return null;
    }

    @Override
    public void addAwarder(YearMeetingSunawarder sunAwarderMsg) {

        sunAwarderDao.insertSelective(sunAwarderMsg);
    }

    @Override
    public SunAwarderDto findAllAwarder() {
        SunAwarderDto sunAwarderDto = new SunAwarderDto();
        YearMeetingSunawarderExample example = new YearMeetingSunawarderExample();
        example.setOrderByClause("award_money desc");
        List<YearMeetingSunawarder> yearMeetingSunawarders = sunAwarderDao.selectByExample(example);
        sunAwarderDto.setList(yearMeetingSunawarders);
        sunAwarderDto.setSunAwarderNum(yearMeetingSunawarders.size());
        return sunAwarderDto;
    }

    @Override
    public void addAwarderAndUpdateSunGood(YearMeetingSunawarderDto awarderMsg,YearMeetingUser user) {
        YearMeetingSunawarder sunawarder = CommonConvertUtil.convert(awarderMsg,new YearMeetingSunawarder());
        sunawarder.setCompany(user.getCompany());
        sunawarder.setUserPic(user.getUserPic());
        addAwarder(sunawarder);
        updateSunGood(sunawarder);
    }

    private void updateSunGood(YearMeetingSunawarder awarderMsg) {
        YearMeetingSungoodsExample example = new YearMeetingSungoodsExample();
        YearMeetingSungoodsExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsMoneyEqualTo(awarderMsg.getAwardMoney());
        List<YearMeetingSungoods> sungoodses = sunGoodsDao.selectByExample(example);
        YearMeetingSungoods yearMeetingSungoods = null;
        if(sungoodses != null && !sungoodses.isEmpty() ){
            yearMeetingSungoods = sungoodses.get(0);
        }
        if(yearMeetingSungoods != null){
            Integer goodsNum = yearMeetingSungoods.getGoodsNum();
            if(goodsNum  >0){
                yearMeetingSungoods.setGoodsNum(goodsNum-1);
                sunGoodsDao.updateByPrimaryKeySelective(yearMeetingSungoods);
            }
        }
    }
}
